package com.qinhailin.portal.qrcode.controller;

import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.QrcodeApi;
import com.qinhailin.common.base.BaseController;
import com.qinhailin.common.model.QrcodeActivity;
import com.qinhailin.common.routes.ControllerBind;
import com.qinhailin.common.util.SignUtil;
import com.qinhailin.portal.qrcode.service.QrcodeActivityService;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * null
 *
 * @author yangguoda
 * @date 2019-12-06
 */
@ControllerBind(path = "/portal/qrcode/qrcodeActivity")
public class QrcodeActivityController extends BaseController {

    @Inject
    QrcodeActivityService service;

    public void index() {
        render("index.html");
    }

    public void list() {
        Record record = new Record();
        record.set("searchName", getPara("searchName"));
        renderJson(service.page(getParaToInt("pageNumber", 1), getParaToInt("pageSize", 10), record));
    }

    public void add() {
        render("add.html");
    }

    public void save() {
        QrcodeActivity entity = getBean(QrcodeActivity.class);
        entity.setId(createUUID());
        entity.save();
        setAttr("qrcodeActivity", entity);
        render("add.html");
    }

    public void edit() {
        setAttr("qrcodeActivity", service.findById(getPara(0)));
        render("edit.html");
    }

    public void update() {
        QrcodeActivity entity = getBean(QrcodeActivity.class);
        entity.update();
        setAttr("qrcodeActivity", entity);
        render("edit.html");
    }

    public void delete() {
        service.deleteByIds(getIds());
        renderJson(suc());
    }

    /**
     * 生成二维码
     */
    public void getQrCode(){
        //传值 或者生成一个参数 进行带参数的二维码生成
        String activityNum = getPara("activityNum",createUUID());
        ApiResult apiResult = QrcodeApi.createPermanent(activityNum);
        String codeAddress = QrcodeApi.getShowQrcodeUrl(apiResult.getStr("ticket"));
        //返回 apiResult.getStr("ticket")
        Map<String,String> map = new HashMap<>();
        map.put("ticket",apiResult.getStr("ticket"));
        map.put("codeAddress",codeAddress);
        map.put("activityNum",activityNum);
        renderJson(map);
    }

    /**
     * 微信关注后的回调
     */
    public void weiXinReturnByQrCode(){
        System.out.println("调用成功了！");
        String signature = getPara("signature");/// 微信加密签名
        String timestamp =getPara("timestamp");/// 时间戳
        String nonce =getPara("nonce"); /// 随机数
        String echostr = getPara("echostr"); // 随机字符串
        if (SignUtil.checkSignature(signature, timestamp, nonce)) {
            Enumeration pNames =getParaNames();
            while (pNames.hasMoreElements()) {
                String name = (String) pNames.nextElement();
                String value = getPara(name);
                String log = "name =" + name + "     value =" + value;
                System.out.println(log);
            }
            //进行参数保存
            System.out.println("校验成功！！！！！！！！！！！！！");
        }
        PrintWriter out = null;
        try {
            out = getResponse().getWriter();
            out.print(echostr);
        } catch (IOException e) {

        }
        out.close();
        out = null;
//        renderJson(suc());
    }



}